package com.mobile.business.mainMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.mobile.business.BaseBusiness;
import com.mobile.mainMenu.model.SysMenuSon;
import com.mobile.mainMenu.model.SysRolePopedom;
import com.mobile.model.SysMenu;
import com.mobile.tools.JsonHelp;

/**
 * 处理菜单的业务逻辑类
 * 
 * @author hf
 * 
 */
public class MainMenuBusinessImpl extends BaseBusiness implements IMainMenuBusiness {

	public void saveSort(Map map) {
		// TODO Auto-generated method stub
		String point = (String)map.get("point");
		
		String dropNodeId = (String)map.get("dropNode");
		String targetNodeId = (String)map.get("target");
		
		if("append".equals(point)){
			this.saveAppend(dropNodeId, targetNodeId);
		}else if("above".equals(point)){
			this.saveAbove(dropNodeId, targetNodeId);
		}else if("below".equals(point)){
			this.saveBelow(dropNodeId, targetNodeId);
		}
	}
	public void saveSort(String jsonData) {
		// TODO Auto-generated method stub
		JsonHelp jsonHelp=new JsonHelp();
		Map map=jsonHelp.getJsonToMap(jsonData);
		this.saveSort(map);
	}
	/**
	 * 把放节点添加到放置于目标节点的前面
	 * @param dropNodeId		放节点id
	 * @param targetNodeId		目标节点id
	 */
	public void saveAbove(String dropNodeId, String targetNodeId) {
		// TODO Auto-generated method stub
		SysMenu sysMenuTarget = (SysMenu)this.baseDAO.get(SysMenu.class, targetNodeId);
		SysMenu sysMenuDrop = (SysMenu)this.baseDAO.get(SysMenu.class, dropNodeId);
		
		DetachedCriteria criteria = DetachedCriteria
				.forClass(SysMenu.class)
				.createAlias("sysMenu", "sysMenu_1")
				.add(Restrictions.eq("sysMenu_1.uniqueCode",sysMenuTarget.getSysMenu().getUniqueCode()))
				.add(Restrictions.le("serialNumber", sysMenuTarget.getSerialNumber()));
		
		List<SysMenu> list=this.baseDAO.findByCriteria(criteria);
		
		Long l=(sysMenuTarget.getSerialNumber()==null?0:sysMenuTarget.getSerialNumber());
		
		SysMenu fatherSysMenu=sysMenuDrop.getSysMenu();
		
		sysMenuDrop.setSerialNumber(l);
		sysMenuDrop.setSysMenu(sysMenuTarget.getSysMenu());
		
		for(SysMenu sysMenu:list){
			sysMenu.setSerialNumber(++l);
		}
		
		this.setIsLeafNode(fatherSysMenu);
	}
	/**
	 * 把节点id为targetNodeId的的节点添加到节点Id为dropNodeId的节点中
	 * @param	dropNodeId		放的节点Id
	 * @param	targetNodeId	拖的目标节点Id
	 */
	public void saveAppend(String dropNodeId, String targetNodeId) {
		// TODO Auto-generated method stub
		SysMenu sysMenuTarget = (SysMenu)this.baseDAO.get(SysMenu.class, targetNodeId);
		SysMenu sysMenuDrop = (SysMenu)this.baseDAO.get(SysMenu.class, dropNodeId);
		
		Long serialNumber=Long.valueOf(this.getMaxSerialNumber(targetNodeId));
		
		sysMenuDrop.setSerialNumber(serialNumber+1);
		//设置放对象的节点为支节点
		sysMenuTarget.setIsLeafNode("0");
		
		SysMenu fatherSysMenu=sysMenuDrop.getSysMenu();
		sysMenuDrop.setSysMenu(sysMenuTarget);		
		this.setIsLeafNode(fatherSysMenu);
	}
	/**
	 * 根据此节点是否又子节点来设置isLeafNode属性
	 * @param sysMenu	节点
	 */
	public void setIsLeafNode(SysMenu sysMenu){
		//SysMenu sysMenu = (SysMenu)this.baseDAO.get(SysMenu.class, nodeId);

		if(!hasChildren(sysMenu.getUniqueCode())){
			sysMenu.setIsLeafNode("1");
		}else{
			sysMenu.setIsLeafNode("0");
		}
	}
	
	/**
	 * 判断nodeId的节点是否又子节点
	 * @param nodeId
	 * @return
	 */
	public boolean hasChildren(String nodeId) {
		// TODO Auto-generated method stub
		//SysMenu sysMenu = (SysMenu)this.baseDAO.get(SysMenu.class, nodeId);
		SysMenu sysMenuTemp = new SysMenu();
		sysMenuTemp.setSysMenu(new SysMenu(nodeId));
		Example sourceExample=Example.create(sysMenuTemp);
		DetachedCriteria criteria = DetachedCriteria
				.forClass(SysMenu.class)
				.add(Restrictions.eq("sysMenu.uniqueCode",nodeId))
				.setProjection(Projections.count("uniqueCode"));
		
		Integer i= (Integer)this.baseDAO.findByCriteria(criteria).get(0);
		
		if(i==0){
			return false;
		}else{
			return true;
		}
		
	}	
	/**
	 * 获得节点ID为nodeId的节点的子集的serialNumber的最大数字
	 * @param	nodeId	节点id
	 */
	public int getMaxSerialNumber(String nodeId){
		DetachedCriteria criteria = DetachedCriteria
				.forClass(SysMenu.class)
				.createAlias("sysMenu", "sysMenu_1")
				.add(Restrictions.eq("sysMenu_1.uniqueCode",nodeId))
				.setProjection(Projections.max("serialNumber"));
		
		List<Object> list=this.baseDAO.findByCriteria(criteria);
		
		int num=0;
		
		if(!list.isEmpty()&&list.get(0)!=null){
			num = Integer.valueOf(list.get(0).toString());
		}
		
		return num;
	}
	/**
	 * 把放节点添加到放置于目标节点的后面
	 * @param dropNodeId		放节点id
	 * @param targetNodeId		目标节点id
	 */
	public void saveBelow(String dropNodeId, String targetNodeId) {
		// TODO Auto-generated method stub
		SysMenu sysMenuTarget = (SysMenu)this.baseDAO.get(SysMenu.class, targetNodeId);
		SysMenu sysMenuDrop = (SysMenu)this.baseDAO.get(SysMenu.class, dropNodeId);
		Long serialNumber=Long.valueOf(this.getMaxSerialNumber(sysMenuTarget.getSysMenu().getUniqueCode()));
		
		SysMenu fatherSysMenu=sysMenuDrop.getSysMenu();
		
		sysMenuDrop.setSysMenu(sysMenuTarget.getSysMenu());
		sysMenuDrop.setSerialNumber(serialNumber+1);
		
		this.setIsLeafNode(fatherSysMenu);
	}
	/**
	 * 根据菜单级别查询
	 * 
	 * @param menuLevel
	 * @return
	 */
	public List<SysMenu> findMenuByMenuLevel(String menuLevel) {
		SysMenu example = new SysMenu();
		example.setMenuLevel(menuLevel);
		List<SysMenu> list = baseDAO.findByExample(example,new String[]{"serialNumber"});
		/*DetachedCriteria criteria=DetachedCriteria.forClass(SysMenu.class);
		DateFilter df=new DateFilter();
		df.setComparison("gt");
		df.setField("createTime");
		df.setValue("10/21/2008");
		DateFilter dfT=new DateFilter();
		dfT.setComparison("lt");
		dfT.setField("createTime");
		dfT.setValue("10/21/2008");
		criteria=df.createCriteria(criteria);
		criteria=dfT.createCriteria(criteria);
		List<SysMenu> list = baseDAO.findByCriteria(criteria);*/
		return list;
	}
	/**
	 * 根据父菜单读取子菜单
	 * @param uniqueCode	父菜单ID
	 * @return				菜单集
	 */
	public List<SysMenu> findByFatherMenu(String uniqueCode){
		List<SysMenu> list=new ArrayList<SysMenu>();
		SysMenu sysMenu=new SysMenu();
		sysMenu.setSysMenu(new SysMenu(uniqueCode));
		
		SysMenu sysMenuAcc=new SysMenu();
		sysMenuAcc.setUniqueCode(uniqueCode);
		sysMenuAcc.setIsLeafNode("1");
		list=this.baseDAO.findByMyselfAssociatedExample(
					sysMenu, 
					new String[]{"sysMenu"}, 
					new Object[]{new SysMenu(uniqueCode)},
					new String[]{"isLeafNode","serialNumber"});
		return list;
	}
	/**
	 * 
	 * @param menuLevel
	 * @return
	 */
	public List<SysMenu> findMenuByFatherNode(String uniqueCode) {
		SysMenu example = new SysMenu();
		example.setUniqueCode(uniqueCode);
		List<SysMenu> list = baseDAO.findByExample(example);
		return list;
	}

	public List<SysRolePopedom> findMenuByRoleCode(String roleCode,
			String operateType) {
		SysRolePopedom example = new SysRolePopedom();
		example.setRoleCode(roleCode);
		example.setOperateType(operateType);
		List<SysRolePopedom> list = baseDAO.findByExample(example);
		return list;
	}

	public List<SysRolePopedom> findTopMenuByRoleCode(String roleCode) {
		SysRolePopedom example = new SysRolePopedom();
		example.setRoleCode(roleCode);
		example.setOperateType("1");
		List<SysRolePopedom> list = baseDAO.findByExample(example);
		return list;
	}

	public List<SysMenuSon> findSecondMenuByRoleCode(String fatherMenu) {
		SysMenuSon example = new SysMenuSon();
		example.setFatherMenu(fatherMenu);
		example.setMenuLevel("1");
		List<SysMenuSon> list = baseDAO.findByExample(example,
				new String[] { "arrangeId" });
		return list;
	}

	public List<SysMenuSon> findSecondMenuByRoleCode(String fatherMenu,
			String superMenu) {
		SysMenuSon example = new SysMenuSon();
		example.setFatherMenu(fatherMenu);
		example.setSuperMenu(superMenu);
		example.setMenuLevel("2");
		List<SysMenuSon> list = baseDAO.findByExample(example,
				new String[] { "arrangeId" });
		return list;
	}
}
